#include <iostream>
#include <cstdlib>
#include <time.h>

/*
*	BenchsPLL  - Matrix sum
*	This program receives 2 argments: The matrix's size N and a number X.
*	It generates random values to fill the matrix from 0 to X
*/


class Matrix{
private:
    int size;
    int **matrix;
public:
    Matrix();
    ~Matrix();
    void Alloc(int);    
    void Start(int);
    void Sum(Matrix*);
    int Get(int, int);
    void Show();
};

Matrix::Matrix(){
    size = 0;
    
}

int Matrix::Get(int x, int y){
    return matrix[x][y];
}

void Matrix::Alloc(int value){
    size = value;
    matrix = new int*[size];
    for (int i = 0; i < size; i++)
        matrix[i] = new int[size];
}

void Matrix::Start(int max){
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] = rand() % max;
}

void Matrix::Sum(Matrix *M){
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] += M->Get(i,j);
}

void Matrix::Show(){
    std::cout << "\n===================\n";
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            std::cout << matrix[i][j] << " ";
        }
        std::cout<<"\n";
    }  
}

Matrix::~Matrix(){
    for (int i = 0; i < size; i++){
        delete [] matrix[i];
    }
    delete [] matrix;
}



int main(int argc, char *argv[])
{
    if (argc < 2){
        std::cerr<<"Error. This program needs 2 argments, the matrix's size and the max value.\n";
        return 1;
    }

    Matrix MA;
    Matrix MB;
    int size = atoi(argv[1]);
    int max =  atoi(argv[2]);
    srand(time(NULL));
    MA.Alloc(size); // O(n²)
    MA.Start(max); // O(n²)
    
    MA.Show(); // O(n²)
    
    MB.Alloc(size); // O(n²)
    MB.Start(max); // O(n²)
    MB.Show(); // O(n²)
    
    MA.Sum(&MB); // O(n²)
    MA.Show();
}
